package com.zrq.jwtdemo.author.controller;

import com.zrq.jwtdemo.author.dto.result.LoginResult;
import com.zrq.jwtdemo.core.result.APIResponse;
import com.zrq.jwtdemo.core.utils.JwtUtils;
import com.zrq.jwtdemo.author.dto.request.LoginRequest;
import com.zrq.jwtdemo.author.service.LoginService;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * @Author:
 * @CreateDate: 2020/12/3
 * @Version: 类说明：
 */
@RestController
@RequestMapping("/test")
@AllArgsConstructor
@Validated
@Slf4j
@Api(value = "登录控制器",tags = "登录控制器")
public class JwtController {

    private final JwtUtils jwtUtils;

    private final LoginService loginService;


    @PostMapping(value = "/login")
    @ApiOperation(value = "登录",notes = "登录")
    public APIResponse<LoginResult> login(@RequestBody @Validated LoginRequest request) throws UnsupportedEncodingException {
        return APIResponse.makeSuccess(loginService.login(request));
    }

    @PostMapping(value = "/index1")
    public APIResponse<List<LoginRequest>> index() {
        List<LoginRequest> list = new ArrayList<>();
        return APIResponse.makeSuccess(list);
    }

    @GetMapping("/get")
    public APIResponse<String> get() {
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId", 12);
        claims.put("messages", "信息");
        String token = jwtUtils.getToken(claims);
        Object verify = jwtUtils.verify("eyJhbGciOiJIUzI1NiJ9.eyJtZXNzYWdlcyI6IuS_oeaBryIsImV4cCI6MTYwNzUzMzEzNywidXNlcklkIjoxMn0.qzz2o5OQaX-_Vf5OMAIBCQqp7phIYXIBMt0fABfnN3g");
        System.out.println(verify);
        return APIResponse.makeSuccess(token);
    }

    @PostMapping("/index")
    public APIResponse<List<String>> test() {

        String key = "@SE!SGDS%Q&";
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId", Math.random());
        claims.put("messages", "信息");

        Calendar intesy = Calendar.getInstance();

        //在当前时间中加上一段时间，加300秒
        intesy.add(Calendar.SECOND,300);
        //生成token
        String token = Jwts.builder()
                .setClaims(claims)
                .setExpiration(intesy.getTime())
                .signWith(SignatureAlgorithm.HS256, key).compact();
        List<String> list = new ArrayList<>();

        //获取token信息
        Claims body = Jwts.parser()
                .setSigningKey(key)
                .parseClaimsJws(token).getBody();
        list.add(token);
        list.add(body.get("userId").toString());
        list.add(body.get("messages").toString());

        log.info(list.toString());
        return APIResponse.makeSuccess(list);

//        throw new AppException(Constant.ERROR_CODE,"出错了");
    }





}
